package com.mobiusws.general;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class CellMapBean
{
	//public enum MAP_DATA_TYPE
	//{
	//	CELL_QUALITY, CELL_TRAFFIC, BYPASS_DAILY, BYPASS_MONTHLY
	//}
	public static String CONTEXT_PATH;
	
	public static void Init()
	{
		//String strProxool=CONTEXT_PATH+"/WEB-INF/proxool_map.properties";
		
		//com.mobiusws.map.App.Init(strProxool);
	}
	
	public static String CellMapGenerator(int iDataType, String strDate)
	{
		//int iDataType=0;
		//String strDate="20090401";
		String outputMapFileDirectory=CONTEXT_PATH+"/data";
		
		return com.mobiusws.map.App.MapGenerator(iDataType, strDate, outputMapFileDirectory);
	}
	
	
	public static Connection GetDBConnection() {
		Context ctx = null;
		Connection conn = null;

		try {
			ctx = new InitialContext();
			DataSource ds = (DataSource) ctx
					.lookup("java:comp/env/jdbc/dbxDataSource");
			conn = ds.getConnection();
		} catch (SQLException e) {
			e.getMessage();
		} catch (NamingException e) {
			e.getMessage();
		} finally {
			if (conn == null) {
				System.out.println("Didn't get connection, which probably means that no Driver accepted the URL");
			} else {
				
			}
		}

		return conn;
	}
	
	public static void CloseDBConnection(Connection conn)
	{
		try
		{
			// Check to see we actually got a connection before we attempt to
			// close it.
			if (conn != null)
			{
				conn.close();
			}
		}
		catch (SQLException e)
		{
			System.out.println("Problem closing connection");
		}
	}
	
	public static String DBQueryCellIDInfo(String cellID)
	{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;

		String strRet="";
		try
		{
			conn = GetDBConnection();
			if (conn == null)
			{
				return null;
			}
			stmt = conn.createStatement();
			// Get the hourly Bypass list STATUS=0. {STATUS=1(daily)}
			rs = stmt.executeQuery("SELECT y, x, region, village, commune, azimuth from cell_id_info where cell_id="+cellID);
			if (rs.next())
			{
				strRet+=rs.getString(1)+";";
				strRet+=rs.getString(2)+";";
				strRet+=rs.getString(3)+";";
				strRet+=rs.getString(4)+";";
				strRet+=rs.getString(5)+";";
				strRet+=rs.getString(6);
			}

			if(rs !=null )  rs.close();
			if(stmt !=null )  stmt.close();
		}
		catch (SQLException e)
		{
			System.out.println(e.getMessage());
		}
		finally
		{
			CloseDBConnection(conn);
		}

		System.out.println("DBQueryCellIDInfo Result="+strRet);
		
		return strRet;
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		//CellMapGenerator();
	}

}
